package algorithms.leaning.class30;

import common.bean.TreeNode;
import common.util.MyUtil;

/**
 * 给定一棵二叉树的头节点head
 * 求以head为头的树中，最小深度是多少？
 *
 * @author guichang
 * @date 2021/7/3
 */

@SuppressWarnings("all")
public class Code3_二叉树_最小深度_套路 {

    public static void main(String[] args) {
        TreeNode treeNode = MyUtil.createTreeNode(1, 2, 3, 4, 5, 6, 7);
        MyUtil.print(minDepth(treeNode));
    }

    public static int minDepth(TreeNode head) {
        if (head == null) {
            return -1;
        }
        return process(head).minDepth;
    }

    public static Info process(TreeNode treeNode) {
        if (treeNode.left == null && treeNode.right == null) {
            return new Info(1);
        }
        Info leftI = process(treeNode.left);
        Info rightI = process(treeNode.right);
        return new Info(1 + Math.min(leftI.minDepth, rightI.minDepth));

    }

    public static class Info {
        int minDepth;

        public Info(int minDepth) {
            this.minDepth = minDepth;
        }
    }


}